import Vue from 'vue'
import vuex from '@/store'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

// const home = r => require.ensure([], () => r(require('../page/home/home')), 'home');

import notFound from '@/views/home/children/404'
import home from '@/views/home/home'
import index from '@/views/home/children/index'
import channel from '@/views/home/children/channel'

import video from '@/views/home/children/video'
import ip from '@/views/home/children/ip/ip'
import ipList from '@/views/home/children/ip/ip-list'
import subject from '@/views/home/children/subject/subject'
import subjectList from '@/views/home/children/subject/subject-list'
import topicList from '@/views/home/children/Topic-list'
import ranking from '@/views/home/children/ranking'
import user from '@/views/home/children/user/user'
import store from '@/views/home/children/user/children/store'
import follow from '@/views/home/children/user/children/follow'
import userInfo from '@/views/home/children/user/children/userInfo'

Vue.use(Router)
const scrollBehavior = (to, from, savedPosition) => {
	if (savedPosition) {
		return savedPosition
	} else {
		return { x: 0, y: 0 }
	}
}

export default new Router({
	// mode: 'history', // 去掉'#'号
	// linkActiveClass: 'active',
	base: __dirname,
	scrollBehavior,
	routes: [
		{
			path: '/',
			component: home,
			children: [
				{
					path: '404',
					component: notFound,
					hidden: true
				},
				{ path: '', component: index },
				{ path: 'channel/:menu_id',component: channel},
				{ path: 'video/:aid', component: video, name: 'video', alias: ['channel/:menu_id/:aid', 'ip/v/:aid', 'subject/v/:aid', 'topic/v/:aid'] },
				{ path: 'ip', component: ip, name: 'ip',},
				{ path: 'ip/:ip_id', component: ipList, name: 'ipList' },
				{ path: 'subject', component: subject, name: 'subject' },
				{ path: 'subject/:subject_id', component: subjectList, name: 'subjectList' },
				{ path: 'topicList/:topic_id', component: topicList, name: 'topicList' },
				{ path: 'ranking', component: ranking},
				{ 
					path: 'user', 
					component: user,
					redirect: '/store',
					children: [
						{ 
							path: '/store',
							component: store,
							beforeEnter: (to,from,next)=> {   //导航守卫
								if(vuex.state.login){
									next();
								}else{
									next({path: '/'});
									vuex.commit('DIALOG_LOGIN');
								}
							}
						},
						{ 
							path: '/follow', 
							component: follow,
							beforeEnter: (to,from,next)=> {   //导航守卫
								if(vuex.state.login){
									next();
								}else{
									next({path: '/'});
									vuex.commit('DIALOG_LOGIN');
								}
							}
						},
						{ 
							path: '/userInfo',
							component: userInfo,
							beforeEnter: (to,from,next)=> {   //导航守卫
								if(vuex.state.login){
									next();
								}else{
									next({path: '/'});
									vuex.commit('DIALOG_LOGIN');
								}
							}
						 }
					]
				},
			]		
		},
		{
			path: '*',
			redirect: { path: '/404' }
		}
	],
})
